﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Telerik.Web.UI;
using System.Web.UI.HtmlControls;
using Runda.CMS.Common.Utility;
using System.Data;

namespace Runda.CMS.Portal.Budget
{
    public partial class SalesBudgetEdit_2 : System.Web.UI.Page
    {
        public Guid? BudgetID
        {
            get {
                if (ViewState["BudgetID"] == null) return null;
                return new Guid(ViewState["BudgetID"].ToString()); 
            }
            set { ViewState["BudgetID"] = value; }
        }

        public Guid AssistID
        {
            get
            {
                if (ViewState["AssistID"] != null)
                {
                    return new Guid(ViewState["AssistID"].ToString());
                }
                else if (Request.QueryString["AssistID"] != null)
                {
                    ViewState["AssistID"] = Request.QueryString["AssistID"];
                }

                return new Guid(ViewState["AssistID"].ToString());
            }
            set { ViewState["AssistID"] = value; }
        }

        public int BudgetYear
        {
            get { return Convert.ToInt32(Request.QueryString["BudgetYear"]); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["BudgetID"] == null)//新增状态
                { 
                    LoadRadTree("");
                }
                else //修改状态
                {
                    BudgetID = new Guid(Request.QueryString["BudgetID"]);
                    InitializeFromData();
                }
                if (Request.QueryString["customername"] != null)
                {
                    lblCustomername.Text = Request.QueryString["customername"].ToString();
                }

                
            }
        }

        private void InitializeFromData()
        {
            BudgetBll bll = new BudgetBll();
            BGSalesBudgetsEntity entity = bll.Get<BGSalesBudgetsEntity>(BudgetID.Value);
            if (entity != null)
            {
                LoadRadTree(entity.ProductCategoryID.ToString());
                //txtInstrumentModel.Text = entity.productModel;

                Button1_Click(null, null);
                txtInstrumentModel.SelectedValue = entity.productModel;
                txtPrice.Text = string.Format("{0:N}", entity.unitPrice);
                txtRemark.Text = entity.Memo;
                txtQ1Count.Text = entity.Q1quantity.ToString();
                txtQ2Count.Text = entity.Q2quantity.ToString();
                txtQ3Count.Text = entity.Q3quantity.ToString();
                txtQ4Count.Text = entity.Q4quantity.ToString();
                txtTotalCount.Text = entity.TotalQuantity.ToString();
                hidSalesAgencyID.Value = entity.SalesAgencyID.ToString();

                txtSalesBudget1_F.Text = entity.SalesBudget1_F.ToString();
                txtSalesBudget1_T.Text = entity.SalesBudget1_T.ToString();
                txtReward1.Text = entity.Reward1.ToString();
                txtSalesBudget2_F.Text = entity.SalesBudget2_F.ToString();
                txtSalesBudget2_T.Text = entity.SalesBudget2_T.ToString();
                txtReward2.Text = entity.Reward2.ToString();
                txtSalesBudget3_F.Text = entity.SalesBudget3_F.ToString();
                txtSalesBudget3_T.Text = entity.SalesBudget3_T.ToString();
                txtReward3.Text = entity.Reward3.ToString();
                txtSalesBudget4_F.Text = entity.SalesBudget4_F.ToString();
                txtSalesBudget4_T.Text = entity.SalesBudget4_T.ToString();
                txtReward4.Text = entity.Reward4.ToString();
                txtSalesBudget5_F.Text = entity.SalesBudget5_F.ToString();
                txtSalesBudget5_T.Text = entity.SalesBudget5_T.ToString();
                txtReward5.Text = entity.Reward5.ToString();
                txtSalesBudget6_F.Text = entity.SalesBudget6_F.ToString();
                txtSalesBudget6_T.Text = entity.SalesBudget6_T.ToString();
                txtFine1.Text = entity.Fine1.ToString();
                txtSalesBudget7_F.Text = entity.SalesBudget7_F.ToString();
                txtSalesBudget7_T.Text = entity.SalesBudget7_T.ToString();
                txtFine2.Text = entity.Fine2.ToString();
                txtSalesBudget8_F.Text = entity.SalesBudget8_F.ToString();
                txtSalesBudget8_T.Text = entity.SalesBudget8_T.ToString();
                txtFine3.Text = entity.Fine3.ToString();
                txtSalesBudget9_F.Text = entity.SalesBudget9_F.ToString();
                txtSalesBudget9_T.Text = entity.SalesBudget9_T.ToString();
                txtFine4.Text = entity.Fine4.ToString();
                txtSalesBudget10_F.Text = entity.SalesBudget10_F.ToString();
                txtSalesBudget10_T.Text = entity.SalesBudget10_T.ToString();
                txtFine5.Text = entity.Fine5.ToString();

                CtrtCustomerEntity CustomerEnt = bll.Get<CtrtCustomerEntity>(entity.SalesAgencyID);
                if (CustomerEnt != null)
                    txtCustomer.Text = CustomerEnt.CustomerName;
                if (entity.SalesAgencyIsTrue == "Y")
                    this.chbSaleAgencyIsTrue.Checked = true;

                if (entity.BudgetSubmmitStatus == 1) { lnkbtnSaveAndClose.Visible = false; }

                //txtInstrumentModel.Enabled = false;
                //txtPrice.Enabled = false;
                //txtRemark.Enabled = false;
                //txtQ1Count.Enabled = false;
                //txtQ2Count.Enabled = false;
                //txtQ3Count.Enabled = false;
                //txtQ4Count.Enabled = false;
                //txtTotalCount.Enabled = false;
                //txtCustomer.Enabled = false;
                //chbSaleAgencyIsTrue.Enabled = false;

                AssistID = entity.AssistID;

                List<BGSalesBudgetsByMonthEntity> list = bll.GetAll<BGSalesBudgetsByMonthEntity>(" and BudgetID='" + BudgetID.Value + "'", " order by BudgetMonth");
                gridItems.DataSource = list;
                gridItems.DataBind();
            }
        }

        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            if (!CheckData()) { return; }
            BudgetBll bll = new BudgetBll();
            BGSalesBudgetsEntity entity = null;
            if (BudgetID.HasValue)
            {
                entity = bll.Get<BGSalesBudgetsEntity>(BudgetID.Value);
            }
            else
            {
                entity = new BGSalesBudgetsEntity();
                entity.AssistID = AssistID;
                entity.BudgetID = Guid.NewGuid();
                entity.productType = Request.QueryString["Type"];

                BudgetID = entity.BudgetID;
            }
            entity.salesBudgetType = "1"; //1,销售预算2,投放预算
            if (Text1.Value != "")
            {
                Guid ID = new Guid(Text1.Value);
                CtrtInstrumentCategoryEntity instrmentEntity = bll.Get<CtrtInstrumentCategoryEntity>(ID);
                if (instrmentEntity != null)
                {
                    entity.productCategoryCode = instrmentEntity.categoryCode;
                    entity.ProductCategoryID = instrmentEntity.categoryGUID;
                }
            }

            if (hidSalesAgencyID.Value != "" && txtCustomer.Text.Trim() != "")
            {
                entity.SalesAgencyID = new Guid(hidSalesAgencyID.Value);
                entity.SalesAgencyIsTrue = chbSaleAgencyIsTrue.Checked ? "Y" : "N";
            }
            else
            {
                entity.SalesAgencyID = Guid.Empty;
                entity.SalesAgencyIsTrue = null;
            }


            entity.unitPrice = txtPrice.Text == "" ? 0 : Convert.ToDecimal(txtPrice.Text.Trim());
            
            entity.Q1quantity = txtQ1Count.Text == "" ? 0 : Convert.ToDecimal(txtQ1Count.Text);
            entity.Q1Amount = entity.unitPrice * entity.Q1quantity;

            entity.Q2quantity = txtQ2Count.Text == "" ? 0 : Convert.ToDecimal(txtQ2Count.Text);
            entity.Q2Amount = entity.unitPrice * entity.Q2quantity;

            entity.Q3quantity = txtQ3Count.Text == "" ? 0 : Convert.ToDecimal(txtQ3Count.Text);
            entity.Q3Amount = entity.unitPrice * entity.Q3quantity;

            entity.Q4quantity = txtQ4Count.Text == "" ? 0 : Convert.ToDecimal(txtQ4Count.Text);
            entity.Q4Amount = entity.unitPrice * entity.Q4quantity;

            entity.TotalQuantity = entity.Q1quantity + entity.Q2quantity + entity.Q3quantity + entity.Q4quantity;
            entity.TotalAmount = entity.unitPrice * (entity.TotalQuantity);

            entity.BudgetYear = BudgetYear.ToString();
            entity.productModel = txtInstrumentModel.SelectedValue.ToString();
            entity.Memo = txtRemark.Text;

            entity.SalesBudget1_F = txtSalesBudget1_F.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget1_F.Text);
            entity.SalesBudget1_T = txtSalesBudget1_T.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget1_T.Text);
            entity.Reward1 = txtReward1.Text == "" ? 0 : Convert.ToDecimal(txtReward1.Text);
            entity.SalesBudget2_F = txtSalesBudget2_F.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget2_F.Text);
            entity.SalesBudget2_T = txtSalesBudget2_T.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget2_T.Text);
            entity.Reward2 = txtReward2.Text == "" ? 0 : Convert.ToDecimal(txtReward2.Text);
            entity.SalesBudget3_F = txtSalesBudget3_F.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget3_F.Text);
            entity.SalesBudget3_T = txtSalesBudget3_T.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget3_T.Text);
            entity.Reward1 = txtReward3.Text == "" ? 0 : Convert.ToDecimal(txtReward3.Text);
            entity.SalesBudget4_F = txtSalesBudget4_F.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget4_F.Text);
            entity.SalesBudget4_T = txtSalesBudget4_T.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget4_T.Text);
            entity.Reward4 = txtReward4.Text == "" ? 0 : Convert.ToDecimal(txtReward4.Text);
            entity.SalesBudget5_F = txtSalesBudget5_F.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget5_F.Text);
            entity.SalesBudget5_T = txtSalesBudget5_T.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget5_T.Text);
            entity.Reward5 = txtReward5.Text == "" ? 0 : Convert.ToDecimal(txtReward5.Text);
            entity.SalesBudget6_F = txtSalesBudget6_F.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget6_F.Text);
            entity.SalesBudget6_T = txtSalesBudget6_T.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget6_T.Text);
            entity.Fine1 = txtFine1.Text == "" ? 0 : Convert.ToDecimal(txtFine1.Text);
            entity.SalesBudget7_F = txtSalesBudget7_F.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget7_F.Text);
            entity.SalesBudget7_T = txtSalesBudget7_T.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget7_T.Text);
            entity.Fine2 = txtFine2.Text == "" ? 0 : Convert.ToDecimal(txtFine2.Text);
            entity.SalesBudget8_F = txtSalesBudget8_F.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget8_F.Text);
            entity.SalesBudget8_T = txtSalesBudget8_T.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget8_T.Text);
            entity.Fine3 = txtFine3.Text == "" ? 0 : Convert.ToDecimal(txtFine3.Text);
            entity.SalesBudget9_F = txtSalesBudget9_F.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget9_F.Text);
            entity.SalesBudget9_T = txtSalesBudget9_T.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget9_T.Text);
            entity.Fine4 = txtFine4.Text == "" ? 0 : Convert.ToDecimal(txtFine4.Text);
            entity.SalesBudget10_F = txtSalesBudget10_F.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget10_F.Text);
            entity.SalesBudget10_T = txtSalesBudget10_T.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget10_T.Text);
            entity.Fine5 = txtFine5.Text == "" ? 0 : Convert.ToDecimal(txtFine5.Text);

            List<BGSalesBudgetsByMonthEntity> list = GetBudgetByMonthManual();
            decimal Quantity = 0;
            decimal Amount = 0;
            foreach (BGSalesBudgetsByMonthEntity ent in list)
            {
                Quantity += ent.quantity;
                Amount += ent.Amount;
                ent.BudgetID = BudgetID.Value;

            }
            
            bll.SaveSalesBudgets(entity, list);

            if (Request.QueryString["Type"] == "1")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功!');window.close();window.opener.document.getElementById('ctl00_cphBody_btnRefersh1').click();", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功!');window.close();window.opener.document.getElementById('ctl00_cphBody_btnRefersh3').click();", true);
            }
        }

        protected void lnkbtnSaveAndCloseEx_Click(object sender, EventArgs e)
        {
            BudgetBll bll = new BudgetBll();
            BGSalesBudgetsEntity entity = null;
            if (BudgetID.HasValue)
            {
                entity = bll.Get<BGSalesBudgetsEntity>(BudgetID.Value);
            }
            entity.SalesBudget1_F = txtSalesBudget1_F.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget1_F.Text);
            entity.SalesBudget1_T = txtSalesBudget1_T.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget1_T.Text);
            entity.Reward1 = txtReward1.Text == "" ? 0 : Convert.ToDecimal(txtReward1.Text);
            entity.SalesBudget2_F = txtSalesBudget2_F.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget2_F.Text);
            entity.SalesBudget2_T = txtSalesBudget2_T.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget2_T.Text);
            entity.Reward2 = txtReward2.Text == "" ? 0 : Convert.ToDecimal(txtReward2.Text);
            entity.SalesBudget3_F = txtSalesBudget3_F.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget3_F.Text);
            entity.SalesBudget3_T = txtSalesBudget3_T.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget3_T.Text);
            entity.Reward3 = txtReward3.Text == "" ? 0 : Convert.ToDecimal(txtReward3.Text);
            entity.SalesBudget4_F = txtSalesBudget4_F.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget4_F.Text);
            entity.SalesBudget4_T = txtSalesBudget4_T.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget4_T.Text);
            entity.Reward4 = txtReward4.Text == "" ? 0 : Convert.ToDecimal(txtReward4.Text);
            entity.SalesBudget5_F = txtSalesBudget5_F.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget5_F.Text);
            entity.SalesBudget5_T = txtSalesBudget5_T.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget5_T.Text);
            entity.Reward5 = txtReward5.Text == "" ? 0 : Convert.ToDecimal(txtReward5.Text);
            entity.SalesBudget6_F = txtSalesBudget6_F.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget6_F.Text);
            entity.SalesBudget6_T = txtSalesBudget6_T.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget6_T.Text);
            entity.Fine1 = txtFine1.Text == "" ? 0 : Convert.ToDecimal(txtFine1.Text);
            entity.SalesBudget7_F = txtSalesBudget7_F.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget7_F.Text);
            entity.SalesBudget7_T = txtSalesBudget7_T.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget7_T.Text);
            entity.Fine2 = txtFine2.Text == "" ? 0 : Convert.ToDecimal(txtFine2.Text);
            entity.SalesBudget8_F = txtSalesBudget8_F.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget8_F.Text);
            entity.SalesBudget8_T = txtSalesBudget8_T.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget8_T.Text);
            entity.Fine3 = txtFine3.Text == "" ? 0 : Convert.ToDecimal(txtFine3.Text);
            entity.SalesBudget9_F = txtSalesBudget9_F.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget9_F.Text);
            entity.SalesBudget9_T = txtSalesBudget9_T.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget9_T.Text);
            entity.Fine4 = txtFine4.Text == "" ? 0 : Convert.ToDecimal(txtFine4.Text);
            entity.SalesBudget10_F = txtSalesBudget10_F.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget10_F.Text);
            entity.SalesBudget10_T = txtSalesBudget10_T.Text == "" ? 0 : Convert.ToInt32(txtSalesBudget10_T.Text);
            entity.Fine5 = txtFine5.Text == "" ? 0 : Convert.ToDecimal(txtFine5.Text);

            bll.InsertOrUpdate(entity);

            if (Request.QueryString["Type"] == "1")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功!');window.close();window.opener.document.getElementById('ctl00_cphBody_btnRefersh1').click();", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功!');window.close();window.opener.document.getElementById('ctl00_cphBody_btnRefersh3').click();", true);
            }
        }

        protected void btnCalculate_Click(object sender, EventArgs e)
        {
            List<BGSalesBudgetsByMonthEntity> list = GetBudgetByMonthAuto();
            //rebind
            gridItems.DataSource = list;
            gridItems.DataBind();

            decimal Quantity = 0;
            decimal Amount = 0; 
            foreach (BGSalesBudgetsByMonthEntity entity in list)
            {
                Quantity += entity.quantity;
                
            }

            txtTotalCount.Text = Quantity.ToString();
        }

  
        public List<BGSalesBudgetsByMonthEntity> GetBudgetByMonthAuto()
        {
            List<BGSalesBudgetsByMonthEntity> list = new List<BGSalesBudgetsByMonthEntity>();

            BGSalesBudgetsByMonthEntity entity = null;

            for (int i = 1; i < 13; i++)
                {
                    entity = new BGSalesBudgetsByMonthEntity();
                    entity.MonthBugetID = Guid.NewGuid();
                    entity.AssistID = AssistID;
                    entity.BudgetMonth = BudgetYear + i.ToString().PadLeft(2, '0');
                    entity.quantity = 0;

                    list.Add(entity);
                }
            
            return list;
        }

        public List<BGSalesBudgetsByMonthEntity> GetBudgetByMonthManual()
        {
            List<BGSalesBudgetsByMonthEntity> list = new List<BGSalesBudgetsByMonthEntity>();
            decimal price = 0;
            decimal.TryParse(txtPrice.Text.TrimEnd(), out price);

            if (gridItems.Items.Count > 0)
            {
                BGSalesBudgetsByMonthEntity entity = null;
                foreach (Telerik.WebControls.GridDataItem item in gridItems.Items)
                {
                    if (item.ItemType == Telerik.WebControls.GridItemType.Item || item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
                    {
                        Label txtBudgetMonth = item.FindControl("txtBudgetMonth") as Label;
                        TextBox txtCount = item.FindControl("txtCount") as TextBox;
                        
                        entity = new BGSalesBudgetsByMonthEntity();
                        entity.MonthBugetID = Guid.NewGuid();
                        //entity.BudgetID = BudgetID.Value;
                        entity.AssistID = AssistID;
                        entity.BudgetMonth = txtBudgetMonth.Text;
                        //decimal Quantity = string.IsNullOrEmpty(txtQ4Count.Text.Trim()) ? 0 : Convert.ToDecimal(txtQ4Count.Text.Trim());
                        decimal Quantity = 0;
                        decimal.TryParse(txtCount.Text.Trim(), out Quantity);

                        entity.quantity = Quantity;

                        entity.Amount = price * Quantity;
                        list.Add(entity);
                    }
                }
            }
            else
            {
                list = GetBudgetByMonthAuto();
            }

            return list;
        }

        private Boolean CheckData()
        {
            Boolean rtn = true;

            if (Text1.Value == "" || Text1.Value == null)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('请选择产品类别!');", true);
                rtn = false;
                return rtn;
            }

            BudgetBll bll = new BudgetBll();
            Guid ID = new Guid(Text1.Value);
            CtrtInstrumentCategoryEntity instrmentEntity = bll.Get<CtrtInstrumentCategoryEntity>(ID);
            if (instrmentEntity != null)
            {
                if (instrmentEntity.categoryCode.Length < 6)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('请选择产品类别!');", true);
                    rtn = false;
                    return rtn;
                }
            }

            if (txtInstrumentModel.SelectedValue == "" || txtInstrumentModel.SelectedValue == null)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('请选择产品型号!');", true);
                rtn = false;
                return rtn;
            }

            if (txtPrice.Text == "")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('请输入销售单价!');", true);
                rtn = false;
                return rtn;
            }

            if (txtQ1Count.Text.Trim() == "" && txtQ2Count.Text.Trim() == "" && txtQ3Count.Text.Trim() == "" && txtQ4Count.Text.Trim() == "")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('请输入季度预算值!');", true);
                rtn = false;
                return rtn;
            }

            if (gridItems.Items.Count > 0)
            {
                int Q1quantity = 0;
                int Q2quantity = 0;
                int Q3quantity = 0;
                int Q4quantity = 0;
                int quantity = 0;
                int Q1quantityTmp = 0;
                int Q2quantityTmp = 0;
                int Q3quantityTmp = 0;
                int Q4quantityTmp = 0;
                int currentrow = 0;

                foreach (Telerik.WebControls.GridDataItem item in gridItems.Items)
                {
                    if (item.ItemType == Telerik.WebControls.GridItemType.Item || item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
                    {
                        Label txtBudgetMonth = item.FindControl("txtBudgetMonth") as Label;
                        TextBox txtCount = item.FindControl("txtCount") as TextBox;
                        TextBox txtAmount = item.FindControl("txtAmount") as TextBox;
                        currentrow = item.RowIndex / 2;
                        if (currentrow <= 3)
                        {
                            if (int.TryParse(txtCount.Text.Trim(), out quantity)) { Q1quantity += quantity; }
                        }

                        if (currentrow <= 6 && currentrow > 3)
                        {
                            if (int.TryParse(txtCount.Text.Trim(), out quantity)) { Q2quantity += quantity; }
                        }
                        if (currentrow <= 9 && currentrow > 6)
                        {
                            if (int.TryParse(txtCount.Text.Trim(), out quantity)) { Q3quantity += quantity; }
                        }

                        if (currentrow <= 12 && currentrow > 9)
                        {
                            if (int.TryParse(txtCount.Text.Trim(), out quantity)) { Q4quantity += quantity; }
                        }


                    }
                }

                int.TryParse(txtQ1Count.Text.Trim(), out Q1quantityTmp);
                int.TryParse(txtQ2Count.Text.Trim(), out Q2quantityTmp);
                int.TryParse(txtQ3Count.Text.Trim(), out Q3quantityTmp);
                int.TryParse(txtQ4Count.Text.Trim(), out Q4quantityTmp);
                
                if (Q1quantityTmp != Q1quantity) 
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('第一季度仪器销售数量有误!');", true);
                    rtn = false; }
                if (Q2quantityTmp != Q2quantity)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('第二季度仪器销售数量有误!');", true);
                    rtn = false;
                }
                if (Q3quantityTmp != Q3quantity)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('第三季度仪器销售数量有误!');", true);
                    rtn = false;
                }
                if (Q4quantityTmp != Q4quantity)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('第四季度仪器销售数量有误!');", true);
                    rtn = false;
                }
            }
            else { ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('请点击按月分摊按钮。');", true); rtn = false; }

            return rtn;
        }

        private void LoadRadTree(string categoryGUID)
        {
            //List<CtrtInstrumentCategoryEntity> categoryEntitys = new CtrtInstrumentCategoryBll().GetAll<CtrtInstrumentCategoryEntity>(" and parentGUID is null", "Order By categoryCode"); 
            DataTable dt = null;
            CtrtInstrumentCategoryBll bll = new CtrtInstrumentCategoryBll();
            dt = bll.GetInstrumentCategory(" and SUBSTRING(categorycode,1,2) = '02' ");
      

            RadTreeView RadTreeProdType = RadComboBox_ProdType.Items[0].FindControl("RadTreeView_ProdType") as RadTreeView;
            //HtmlInputHidden Hidden_ProdTypeID = Page.FindControl("Text1") as HtmlInputHidden;
            //HtmlInputText txtSelectType = Page.FindControl("txt_SelectType") as HtmlInputText;
            if (RadTreeProdType != null)
            {
                RadTreeProdType.DataFieldID = "categoryGUID";
                RadTreeProdType.DataTextField = "categoryName";
                RadTreeProdType.DataValueField = "categoryGUID";
                RadTreeProdType.DataFieldParentID = "parentGUID";
                RadTreeProdType.DataSource = dt;
                RadTreeProdType.DataBind();
            }
            if (categoryGUID != "")
            {
                Text1.Value = categoryGUID;
                txt_SelectType.Value = RadTreeProdType.FindNodeByValue(categoryGUID).Text;

                RadComboBox_ProdType.SelectedItem.Value = RadTreeProdType.FindNodeByValue(categoryGUID).Text;
                RadComboBox_ProdType.SelectedItem.Text = RadTreeProdType.FindNodeByValue(categoryGUID).Text;
                RadComboBox_ProdType.Text = RadTreeProdType.FindNodeByValue(categoryGUID).Text;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            BudgetBll bll = new BudgetBll();

            List<WsmInstrumentInfoEntity> list = bll.GetInstrumentEntities(new Guid(Text1.Value));
            this.txtInstrumentModel.DataSource = list;
            txtInstrumentModel.DataValueField = "InstrumentName";
            txtInstrumentModel.DataTextField = "InstrumentName";
            txtInstrumentModel.DataBind();
            txtInstrumentModel.Items.Add(new ListItem("   ", ""));
            txtInstrumentModel.SelectedValue = "";
        }

    }
}
